package com.androidexplosion.takeitback.gui;

import java.util.ArrayList;

import com.androidexplosion.takeitback.R;
import com.androidexplosion.takeitback.entity.Remind;
import com.androidexplosion.takeitback.provider.RemindDBAdapter;

import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


public class ReminderLite extends Activity {

	private ArrayList<Remind> remindItems;
	private ListView remindList;
	RemindArray remindArray;
	
	private static final String SELECTED_INDEX_KEY = "SELECTED_INDEX_KEY";

	RemindDBAdapter remindAdapter;
	//Cursor remindListCursor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startlist);

		remindList = (ListView)findViewById(R.id.listRemind);

		remindItems = new ArrayList<Remind>();
		int resID = R.layout.remindlist_item;
		remindArray = new RemindArray(this, resID, remindItems);
		remindList.setAdapter(remindArray);

		//registerForContextMenu(remindList);

		remindAdapter = new RemindDBAdapter(this);

		// Open or create the database
		remindAdapter.open();

		populateRemindList();
	}

//	private void restoreUIState() {
//		// Get the activity preferences object.
//		SharedPreferences settings = getPreferences(0);
//
//		// Read the UI state values, specifying default values.
//		String text = settings.getString(OBJECT_KEY, "");
//		long date = settings.getLong(DATE_KEY, 0);
//		Boolean adding = settings.getBoolean(DATE_KEY, false);
//
//		// Restore the UI to the previous state.
//		if (adding) {
//			addNewRemind();
//			// myEditText.setText(text);
//		}
//	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(SELECTED_INDEX_KEY, remindList
				.getSelectedItemPosition());

		super.onSaveInstanceState(outState);
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		int pos = -1;

		if (savedInstanceState != null)
			if (savedInstanceState.containsKey(SELECTED_INDEX_KEY))
				pos = savedInstanceState.getInt(SELECTED_INDEX_KEY, -1);

		remindList.setSelection(pos);
	}

	private void populateRemindList() {
		// Get all remind list items from the database.
		remindItems.clear();

		remindItems = remindAdapter.getAllReminds();
		
		for(int i = 0; i < remindItems.size(); i++)
		{
			remindArray.add(remindItems.get(i));
		}

		// Update the array.
		remindArray.notifyDataSetChanged();
	}

	// --- MENU ---
	static final private int ADD_NEW_REMIND = Menu.FIRST;
	static final private int REMOVE_REMIND = Menu.FIRST + 1;
	static final private int SETTINGS = Menu.FIRST + 2;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		// Create and add new menu items.
		MenuItem itemAdd = menu.add(0, ADD_NEW_REMIND, Menu.NONE,
				R.string.add_new);
		MenuItem itemRem = menu.add(0, REMOVE_REMIND, Menu.NONE,
				R.string.remove);
		MenuItem itemSettings = menu.add(0, SETTINGS, Menu.NONE,
				R.string.settings);

		// Assign icons
		itemAdd.setIcon(R.drawable.add_new_item);
		itemRem.setIcon(R.drawable.remove_item);

		// Allocate shortcuts to each of them.
		itemAdd.setShortcut('0', 'a');
		itemRem.setShortcut('1', 'r');
		itemSettings.setShortcut('2', 's');

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		int index = remindList.getSelectedItemPosition();

		switch (item.getItemId()) {
		case (ADD_NEW_REMIND): {
			addNewRemind();
			return true;
		}

		case (SETTINGS): {
			openSettings();
			return true;
		}
		case (REMOVE_REMIND): {

			removeItem(index);

			return true;
		}
		}

		return false;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);

		 int id = remindList.getSelectedItemPosition();
		
		 MenuItem removeItem = menu.findItem(REMOVE_REMIND);
		 removeItem.setVisible(id > -1);

		return true;
	}

	// --- END MENU ---

	// --- CONTEXT MENU ---

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		menu.setHeaderTitle("Selected To Do Item");
		menu.add(0, REMOVE_REMIND, Menu.NONE, R.string.remove);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		switch (item.getItemId()) {
		case (REMOVE_REMIND): {
			AdapterView.AdapterContextMenuInfo menuInfo;
			menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
			int index = menuInfo.position;

			removeItem(index);
			return true;
		}
		}
		return false;
	}

	// --- END CONTEXT MENU ---

	@Override
	public void onDestroy() {
		super.onDestroy();

		// Close the database
		remindAdapter.close();
	}

	private void addNewRemind() {
		Intent i = new Intent(this, AddRemind.class);
		startActivity(i);
	}
	
	private void openSettings() {
		Intent i = new Intent(this, Settings.class);
		startActivityForResult(i, SETTINGS);
	}
	

	private void removeItem(int _index) {
		Remind remind = remindItems.get(_index);
		remindAdapter.deleteRemind(remind.getRemindID());
		populateRemindList();
	}
	
	@Override
	public void onActivityResult(int reqCode, int resCode, Intent data) {
		super.onActivityResult(reqCode, resCode, data);

		switch (reqCode) {
		case (ADD_NEW_REMIND): {
			if (resCode == Activity.RESULT_OK) {
				populateRemindList();
			}
			break;
		}
		}
	}
	
	
}